Repository navigation
🐛 fix(mcp): bound startup fan-out to stop connect timeouts - #11
Merged
Merged
Conversation
Downstream MCP servers were all connected under one unbounded Promise.all. With ~15 servers, many cold-starting uvx/npx children contended for CPU/IO (a "thundering herd"), pushing the slowest (basic-memory) past the 15s connect timeout. It was then silently dropped from the pool while faster servers stayed available in the same session, with no retry until the host respawned the process. - Bound the startup fan-out to POOL_STARTUP_CONCURRENCY (default 6) via a new mapWithConcurrency helper, so cold-starting children no longer starve each other's connect handshake. - Retry servers that miss the first window serially (POOL_STARTUP_RETRIES, default 1) once the herd has cleared, so a slow server connects on the retry instead of being dropped for the whole process lifetime. - Raise POOL_CONNECT_TIMEOUT_MS default 15s -> 20s for extra headroom. - Report retry-recovered servers with a [via retry] marker. Measured on a 15-server config: the whole pool comes up ~50-70% faster (herd gone) and basic-memory connects in ~3s instead of timing out at 15s (15/15 vs the previous 14/15). Tests: mapWithConcurrency unit tests (order, coverage, concurrency cap, edge cases) and startup-config validation tests; stale getPoolConfig mocks updated to the new schema. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Downstream MCP servers (most visibly
basic-memory) intermittently failed toappear via code-executor, while other servers in the same session were fine.
Root cause confirmed from
mcp-server-code-executor.log:basic-memorywas the slowest server and sat right at the connect timeout,oscillating between ~3s and 15s+ across restarts.
Root cause
All downstream servers were connected under one unbounded
Promise.all.With ~15 servers, many cold-starting
uvx/npxchildren contend for CPU/IO(a thundering herd) and starve each other's connect handshake — the log even
shows a run where all 15 timed out at once (~15s). The slowest server
crossed the 15s deadline, was dropped from the pool, and — with no retry and no
lazy reconnect — stayed missing until the host respawned the process.
Fix
Promise.allwith a newmapWithConcurrencyhelper (sliding window). At mostPOOL_STARTUP_CONCURRENCY(default 6) servers spawn+handshake at once,removing the herd. The helper preserves input order and falls back to serial
on a bad limit (never drops items).
time (
POOL_STARTUP_RETRIES, default 1) after the herd clears, so aslow server gets the machine to itself instead of being dropped for the
process lifetime. Retry-recovered servers are marked
[via retry]in thestartup report.
POOL_CONNECT_TIMEOUT_MSdefault 15s → 20s.New config knobs
POOL_STARTUP_CONCURRENCYPOOL_STARTUP_RETRIESPOOL_CONNECT_TIMEOUT_MSVerification
tests/map-with-concurrency.test.ts(order preservation,full coverage, concurrency-cap enforcement, empty/oversized/
<1limit) +startup-config validation tests. Two stale
getPoolConfigmocks updated tothe new schema.
suite-wide failures are pre-existing filesystem/systemd tests that also fail
on the unmodified baseline.
basic-memory ✓ 3189ms, 15/15 connected, and the whole pool comes up~50–70% faster because the herd is gone (e.g. Context7 4.2s→1.6s,
desktop-commander 5.0s→2.9s, duckdb 3.1s→1.2s — servers whose own config was
untouched, so the speedup is purely the bounded fan-out).
🤖 Generated with Claude Code